package lzj.tools;

import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * HTTP 请求工具类
 */
public class Https {

  /**
   * 未授权：401
   */
  public static final int CODE_UNAUTHORIZED = HttpURLConnection.HTTP_UNAUTHORIZED;

  /**
   * 找不到：404
   */
  public static final int CODE_NOT_FOUND = HttpURLConnection.HTTP_NOT_FOUND;

  /**
   * 不可接受的参数：406
   */
  public static final int CODE_NOT_ACCEPTABLE = HttpURLConnection.HTTP_NOT_ACCEPTABLE;

  /**
   * 请求冲突：409
   */
  public static final int CODE_CONFLICT = HttpURLConnection.HTTP_CONFLICT;

  private static final MediaType JSON = MediaType
      .parse("application/json; chartset=utf-8");
  private static OkHttpClient CLIENT = new OkHttpClient();

  /**
   * 发送 GET 请求
   */
  public static String get(String url) {
    String result = null;

    try {
      final Request request = new Request.Builder().url(url).build();
      final Response response = CLIENT.newCall(request).execute();
      result = response.body().string();
    } catch (Exception e) {
      e.printStackTrace();
    }

    return result;
  }

  /**
   * 发送 POST 请求
   *
   * @param param key=value
   */
  public static String post(String url, String param) {
    String result = null;

    try {
      final RequestBody body = RequestBody.create(JSON, param);
      final Request request = new Request.Builder().url(url).post(body).build();
      final Response response = CLIENT.newCall(request).execute();
      result = response.body().string();
    } catch (Exception e) {
      e.printStackTrace();
    }

    return result;
  }

  /**
   * 发送 POST 请求
   *
   * @param url 请求的url
   * @param headers headers
   * @param params params
   */
  public static String post(String url, Map<String, String> headers,
      Map<String, String> params) {
    String result = null;

    try {

      Request.Builder builder = new Request.Builder().url(url);

      for (Map.Entry<String, String> entry : headers.entrySet()) {
        builder.addHeader(entry.getKey(), entry.getValue());
      }

      FormBody.Builder formBody = new FormBody.Builder();

      for (Map.Entry<String, String> entry : params.entrySet()) {
        formBody.add(entry.getKey(), entry.getValue());
      }

      RequestBody body = formBody.build();

      final Request request = builder.post(body).build();

      final Response response = CLIENT.newCall(request).execute();
      result = response.body().string();

    } catch (Exception e) {
      e.printStackTrace();
    }

    return result;
  }

  /**
   * Ajax 请求成功时，返回信息格式处理
   */
  public static Map<String, Object> success(Object message, Object data) {
    Map<String, Object> map = new HashMap<>();

    map.put("code", 1);
    map.put("message", message);
    map.put("data", data);

    return map;
  }

  /**
   * Ajax 请求失败，一般错误处理
   */
  public static Map<String, Object> failure(Object message, Object data) {
    Map<String, Object> map = new HashMap<>();

    map.put("code", 0);
    map.put("message", message);
    map.put("data", data);

    return map;
  }

  /**
   * Ajax 请求失败，特殊错误处理
   */
  public static Map<String, Object> failure(Object message, Object data,
      int i) {
    Map<String, Object> map = new HashMap<>();

    map.put("code", i);
    map.put("message", message);
    map.put("data", data);

    return map;
  }

}
